package com.shijie.newvideo;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.shuyu.gsyvideoplayer.GSYBaseActivityDetail;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PlayDetailActivity extends GSYBaseActivityDetail<StandardGSYVideoPlayer> {
    StandardGSYVideoPlayer detailPlayer;
    String ids;
    RecyclerView rv_video;
    Context mContext;
    VodDetailListAdapter vodDetailListAdapter;
    List<IdNameBean> videoList = new ArrayList<>();
    String url;

    public static void intentTo(Context context, String ids) {
        Intent intent = new Intent(context, PlayDetailActivity.class);
        Bundle bundle = new Bundle();
        intent.putExtra("ids", ids);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_detail);
        ids = getIntent().getStringExtra("ids");
        mContext = this;

        rv_video = findViewById(R.id.rv_video);
        rv_video.setNestedScrollingEnabled(false);
//        rv_video.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        rv_video.setLayoutManager(new GridLayoutManager(mContext, 4));
        vodDetailListAdapter = new VodDetailListAdapter(mContext, videoList);
        rv_video.setAdapter(vodDetailListAdapter);

        vodDetailListAdapter.setShareOnClickListener(new VodDetailListAdapter.ShareOnClickListener() {
            @Override
            public void shareFlashOnClick(IdNameBean idNameBean) {
//                GSYExoVideoManager.instance().next();
//                ((GSYExo2PlayerView) binding.detailPlayer.getCurrentPlayer()).nextUI();
            }
        });

        detailPlayer = (StandardGSYVideoPlayer) findViewById(R.id.detail_player);
        detailPlayer.getTitleTextView().setVisibility(View.GONE);
        detailPlayer.getBackButton().setVisibility(View.GONE);

        //增加title
        Call<VodBean> call = RetrofitClient.getApiService().getKanshipinglist("detail", ids);
        call.enqueue(new Callback<VodBean>() {
            @Override
            public void onResponse(Call<VodBean> call, Response<VodBean> response) {
                if (response.isSuccessful()) {
                    VodBean vodBean = response.body();
                    Log.e("Network User: ", vodBean.toString());
                    String vod_play_url = vodBean.getList().get(0).getVod_play_url();
                    List<IdNameBean> urlList = ArrayListUtils.extractUrlsList(vod_play_url);
                    if (urlList != null && urlList.size() > 0) {
                        url = urlList.get(0).getUrl();
                        videoList.clear();
                        videoList.addAll(urlList);
                        vodDetailListAdapter.notifyDataSetChanged();
                    }
                    setVideoData();
                } else {
                    Log.e("Network Error: ", response.message());
                }
            }

            @Override
            public void onFailure(Call<VodBean> call, Throwable t) {
                Log.e("Network Error: ", t.getMessage());
            }
        });

        findViewById(R.id.tv_down).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PlayDownActivity.intentTo(mContext, videoList);
            }
        });
    }

    public void setVideoData() {
        initVideoBuilderMode();

        //允许window 的内容可以上移到刘海屏状态栏
        if (getWindow() != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            getWindow().setAttributes(lp);
        }
    }


    @Override
    public StandardGSYVideoPlayer getGSYVideoPlayer() {
        return detailPlayer;
    }

    @Override
    public GSYVideoOptionBuilder getGSYVideoOptionBuilder() {
        //内置封面可参考SampleCoverVideo
        ImageView imageView = new ImageView(this);
        //loadCover(imageView, url);
        return new GSYVideoOptionBuilder().setThumbImageView(imageView).setUrl(url).setCacheWithPlay(true).setVideoTitle("这里是一个竖直方向的视频").setIsTouchWiget(true)
                //.setAutoFullWithSize(true)
                .setRotateViewAuto(false).setLockLand(false).setShowFullAnimation(false)//打开动画
                .setNeedLockFull(true).setSeekRatio(1);
    }

    @Override
    public void clickForFullScreen() {

    }


    /**
     * 是否启动旋转横屏，true表示启动
     */
    @Override
    public boolean getDetailOrientationRotateAuto() {
        return true;
    }

    private void loadCover(ImageView imageView, String url) {
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(R.drawable.ic_launcher);
        Glide.with(this.getApplicationContext()).setDefaultRequestOptions(new RequestOptions().frame(3000000).centerCrop().error(R.drawable.ic_launcher).placeholder(R.drawable.ic_launcher)).load(url).into(imageView);
    }


}